package com.easygem.controller.business;

import com.easygem.business.service.AccountService;
import com.easygem.business.service.ClientTypeService;
import com.easygem.common.model.business.Account;
import com.easygem.common.model.business.ClientType;
import com.easygem.common.model.system.DeptClient;
import com.easygem.common.model.system.Employee;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.business.AccountVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.service.ZoningService;
import com.easygem.system.service.EmployeeService;
import com.easygem.system.service.impl.DeparmentClientServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 何维佳
 * @version 1.0
 * @className AccountController
 * @Description TODO
 * @date 2025/1/13 15:43
 */
@Api(tags = "客户账户管理")
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private ClientTypeService clienttypeService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DeparmentClientServiceImpl deparmentClientService;

    @Autowired
    private ZoningService zoningService;

    /**
     * 客户账户管理
     *
     * @return
     */
    @ApiOperation(value = "客户账户管理", notes = "模糊查询客户账户")
    @GetMapping("/findAccountList")
    @ResponseBody
    public ResponseBean<PageVO<Account>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                  AccountVO accountVO) {
        PageVO<Account> accountList = accountService.findList(pageNum, pageSize, accountVO);
        System.out.println("***************************************"+accountList);
        return ResponseBean.success(accountList);
    }


    /**
     * 客人种类列表
     *
     * @return
     */
    @ApiOperation(value = "资源列表", notes = "模糊查询持有人")
    @GetMapping("/findClienttypeList")
    @ResponseBody
    public List<ClientType> findClienttypeList() {
        List<ClientType> clientTypeList = clienttypeService.findClienttypeList();
        return clientTypeList;
    }

    /**
     * 雇员列表
     *
     * @return
     */
    @ApiOperation(value = "雇员列表", notes = "模糊查询持有人")
    @GetMapping("/findEmployeeList")
    @ResponseBody
    public List<Employee> findEmployeeList() {
        List<Employee> employeeList = employeeService.findEmployeeList();
        return employeeList;
    }

    /**
     * 部门列表
     *
     * @return
     */
    @ApiOperation(value = "部门列表", notes = "模糊查询持有人")
    @GetMapping("/findDeparmentClientList")
    @ResponseBody
    public List<DeptClient> findDeparmentClientList() {
        List<DeptClient> deptClientList = deparmentClientService.findAccountDept();
        return deptClientList;
    }

    /**
     * 查询账户名称是否重名
     *
     * @return
     */
    @ApiOperation(value = "部门列表", notes = "模糊查询持有人")
    @GetMapping("/findAccountCount")
    @ResponseBody
    public int findAccountCount(@RequestParam(value = "accountName") String accountName) {
        int count =accountService.findAccountName(accountName);
        return count;
    }

    /**
     * 添加账户
     *
     * @return
     */
    @ApiOperation(value = "添加资源", notes = "更新")
    @PostMapping("/addAccount")
    @ResponseBody
    @RequiresPermissions("account:addAccount")
    public  ResponseBean<AccountVO>  addAccount(@RequestBody AccountVO accountVO) {
        int result = accountService.addAccount(accountVO);
        Long accountId = accountVO.getAccountId();
        System.out.println("111111111111111111111111111111111111111111111111111"+accountId);
        accountVO.setAccountId(accountId);

        System.out.println(result);

        return ResponseBean.success(accountVO);
    }

    /**
     * 更新资源
     *
     * @return
     */
    @ApiOperation(value = "更新资源", notes = "更新")
    @PostMapping("/updateAccountListById")
    @ResponseBody
    @RequiresPermissions("account:updateAccount")
    public  ResponseBean<AccountVO>  updateResourceInfo(@RequestBody AccountVO accountVO) {
        User user= zoningService.selectAdminUser(accountVO.getAccountName());
        Map<String,Object> map=new HashMap<>();
        map.put("availableSeatsNum",accountVO.getSeatsNumber());
        map.put("deptId",user.getDeptId());
        zoningService.updateAvailableSeatsNum(map);
        int result = accountService.update(accountVO);
        System.out.println(result);
        return ResponseBean.success(accountVO);
    }

    /**
     * 删除资源
     *
     * @return
     */
    @ApiOperation(value = "删除资源", notes = "删除")
    @GetMapping("/deleteAccountById")
    @ResponseBody
    @RequiresPermissions("account:delete")
    public int deleteAccountById(@RequestParam(value = "id") Long id) {
        int result = accountService.delete(id);
        System.out.println(result);
        return result;
    }

    /**
     * 删除资源
     *
     * @return
     */
    @ApiOperation(value = "删除资源", notes = "删除")
    @GetMapping("/deleteUserByAccountName")
    @ResponseBody
    @RequiresPermissions("account:allDelete")
    public int deleteUserByAccountName(@RequestParam(value = "accountName") String accountName) {
        int result = accountService.deleteUserByAccountName(accountName);
        System.out.println(result);
        return result;
    }

    /**
     * 恢复资源
     *
     * @return
     */
    @ApiOperation(value = "删除资源", notes = "删除")
    @GetMapping("/updateUserByAccountName")
    @ResponseBody
    @RequiresPermissions("account:allUpdate")
    public int updateUserByAccountName(@RequestParam(value = "accountName") String accountName) {
        int result = accountService.updateUserByAccountName(accountName);
        System.out.println(result);
        return result;
    }

    /**
     * 检查是否需要更新账户下所有用户的线路类型
     *
     * @return
     */
    @ApiOperation(value = "检查是否需要更改账户线路类型", notes = "检查")
    @PostMapping("/chackCircuritIdIsChange")
    @ResponseBody
    @RequiresPermissions("account:chackCircuritIdIsChange")
    public int chackCircuritIdIsChange(@RequestBody AccountVO accountVO) {
        int result = accountService.chackCircuritIdIsChange(accountVO);
        System.out.println("checkResult"+result);
        return result;
    }

    /**
     * 将账户下的用户的线路类型修改
     *
     * @return
     */
    @ApiOperation(value = "将账户下的用户的线路类型修改", notes = "修改")
    @PostMapping("/updateUserCircuritIdByAccountName")
    @ResponseBody
    @RequiresPermissions("account:updateUserCircuritIdByAccountName")
    public int updateUserCircuritIdByAccountName(@RequestBody AccountVO accountVO) {
        int result = accountService.updateUserCircuritIdByAccountName(accountVO);
        System.out.println("updateResult"+result);
        return result;
    }

}
